﻿;_____________________________________________________________________
;---------------------------------------------------------------------
;	File: PML_PDFsearch
;---------------------------------------------------------------------
;	Group: Overview
;
;		Validates the user's search input and performs a search for the
;		specified PDF file. Search is performed in the PM Apps DRAWINGS
;		public folder, as well as in the old documents folder.
;
;	Group: To Do
;		- Use RegEx search in documents folder so that files with "rev A"
;		  etc. will still be matched and opened
;
;	Group: About
;		General script file information
;
;		- *Written By:* 	Rameen Bakhtiary
;		- *Last Modified:*	11/24/2011
;_____________________________________________________________________
;---------------------------------------------------------------------





;======================================================================
;	Sub: PMF_dwgChk
;
;		This routine is called every time there is a change made to
;		the PDF input search text (ie- each time a letter is typed).
;		This routine ensures that the user has entered at least 4 
;		characters before allowing the search to be run.
;======================================================================
	PMF_dwgChk:
		Gui, Submit, NoHide
		If StrLen(stext) >= 4
			GuiControl, Enable, button1
		Else
			GuiControl, Disable, button1
	Return





;=================================================================
;                          PERFORM THE SEARCH
;=================================================================
	PMF_dwgSearch:   
		
		;______________________________________
		; 	APPEND "9290-" IF ONLY 4 #S ENTERED
		;
		If (StrLen(stext)=4)		
			stext = 9290-%stext%
		
		
		;________________________________________________________________
		; 	FULL PATH OF PDF FILE (ASSUMING IT EXISTS IN DRAWINGS FOLDER)
		;	
		dwgFamily := StrLeft(stext, 4)	;Part family num (first 4 #s)
		dwgFolder := StrLeft(stext, 6) "000"	;Folder Name in DRAWINGS
		dwgFolderPath := epub "\drawings\" dwgFolder	;Full folder path in DRAWINGS
		dwgFullPath := dwgFolderPath "\" stext ".pdf"	 ;Full path to PDF file in DRAWINGS
		
		
		;___________________________________________________________________
		; 	PATH OF PDF IN DOCUMENTS FOLDER (IF NOT IN DRAWINGS, CHECK HERE)
		;
		docFolder := dwgFamily = "9290" ? "9290"
					: dwgFamily = "9381" ? "9381\" SubStr(dwgFolder, 6)
					: dwgFamily = "9450" ? "9450"
					: dwgFamily
		docFolderPath := epub "\documents\" docFolder
		docFullPath := docFolderPath "\" stext ".pdf"
		
		
		;_________________________________________________________
		; 		REGEX STRING USED TO SEARCH FILES; ALLOWS MATCHING
		;		EVEN IF FILE HAS A REVISION IN THE NAME; WILL
		;		OPEN THE LATEST REVISION IF MULTIPLE ARE FOUND.
		;
		;~ Loop, PDFsearch_RegEx := "([a-z-_\.0-9]{9})(?:\s*?(?:(?:Rev\s*?)?([a-z0-9]*?))?)\s*?\.pdf"
		
		
		;__________________________________________________________
		; 	CHECK FOR FILE IN THE POSSIBLE FOLDERS AND RUN IF FOUND
		;
		if fileExist(dwgFullPath){ 
			
			;	Found in DRAWINGS
			;=======================
			gui, destroy
			run, % dwgFullPath
			return
		}else if FileExist(docFullPath){ 
		
			;	Found in documents
			;========================
			gui, destroy
			run, % docFullPath
			return
		}else
			
			;	NOT Found
			;===============
			MsgBox, 4144, PM App Launcher, 
			(Join LTrim
			%stext% was not found in the public drawings archive.`n`n
			Remember that you can add a PDF to the public drawings by  right-clicking it and selecting 'PM TOOLS>>Move To DWGs folder"`n
			)
	Return	





;____________________________________ E N D   O F   F I L E ________________________________________
;%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%&%



















